/**
 * Copyright (c) 2018 软唐 All rights reserved.
 *
 * https://www.softtown.cn
 *
 * 版权所有，侵权必究！
 */

package com.softtown.modules.dapai.controller;

import com.github.pagehelper.PageInfo;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.common.utils.Result;
import com.softtown.modules.dapai.dto.CompMacAddDTO;
import com.softtown.modules.dapai.dto.CompWarehouseOrderAddDTO;
import com.softtown.modules.dapai.dto.CompWarehouseOrderDTO;
import com.softtown.modules.dapai.dto.CompWarehouseOrderDetailDTO;
import com.softtown.modules.dapai.entity.CompMacEntity;
import com.softtown.modules.dapai.entity.CompWarehouseOrderDetailEntity;
import com.softtown.modules.dapai.entity.CompWarehouseOrderEntity;
import com.softtown.modules.dapai.service.CompMacService;
import com.softtown.modules.dapai.service.CompWarehouseOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
* @Author: Ding Ya Yong
* @Description: 入库单
* @date: 2022/1/2 20:47
* @Param:
* @return:
*/
@RestController
@RequestMapping("WarehouseOrder")
@Api(tags="入库单")
public class CompWarehouseOrderController {

    @Resource
    private CompWarehouseOrderService compWarehouseOrderService;

    @Resource
    private CompMacService compMacService;

    @PostMapping("getWarehouseOrderPageList")
    @ApiOperation("获取入库单分页")
    public Result getWarehouseOrderPageList(@RequestBody CompWarehouseOrderDTO compWarehouseOrderDTO) {
        PageInfo<CompWarehouseOrderDTO> compWarehouseOrderDTOPageInfo = compWarehouseOrderService.getWarehouseOrderPageList(compWarehouseOrderDTO);
        return new Result<PageInfo<CompWarehouseOrderDTO>>().ok(compWarehouseOrderDTOPageInfo);
    }



    @PostMapping("getWarehouseOrderById")
    @ApiOperation("获取入库单详情")
    public Result<CompWarehouseOrderAddDTO> getWarehouseOrderById(@RequestBody CompWarehouseOrderDTO compWarehouseOrderDTO) {
        CompWarehouseOrderAddDTO compWarehouseOrderAddDTO = compWarehouseOrderService.getWarehouseOrderById(compWarehouseOrderDTO.getCompId(),compWarehouseOrderDTO.getId());
        return new Result<CompWarehouseOrderAddDTO>().ok(compWarehouseOrderAddDTO);
    }


    @PostMapping("addWarehouseOrder")
    @ApiOperation("添加入库单")
    public Result addWarehouseOrder(@RequestBody CompWarehouseOrderAddDTO compWarehouseOrderAddDTO) {
        Result result = new Result<>();
        CompWarehouseOrderEntity compWarehouseOrderEntity = compWarehouseOrderAddDTO.getCompWarehouseOrderEntity();
        List<CompWarehouseOrderDetailEntity> compWarehouseOrderDetailDTOList =  compWarehouseOrderAddDTO.getCompWarehouseOrderDetailEntityList();
        for (CompWarehouseOrderDetailEntity entity: compWarehouseOrderDetailDTOList){
            CompMacAddDTO compMacAddDTO = compMacService.getMacById(compWarehouseOrderEntity.getCompId(), entity.getMacId());
            CompMacEntity compMacEntity =ConvertUtils
                    .sourceToTarget(compMacAddDTO.getCompMacEntity(), CompMacEntity.class);
            // 出库后,库存数量小于最小库存数量
            if (!StringUtils.isEmpty(compMacEntity)){
                if ((compMacEntity.getMaxInventory() - compMacEntity.getStock()) < entity.getWarehouseNum()){
                    return result.error("库存数量不能小于最小库存数量");
                }else {
                    CompMacEntity compMacEntity1 = new CompMacEntity();
                    compMacEntity1.setId(compMacEntity.getId());
                    compMacEntity1.setCompId(compMacEntity.getCompId());
                    compMacEntity1.setStock(compMacEntity.getStock() + entity.getWarehouseNum());
                    compWarehouseOrderService.updateMacStock(compMacEntity1);
                }
            }else {
                return result.error("设备不存在");
            }


        }
        compWarehouseOrderService.addWarehouseOrder(compWarehouseOrderAddDTO);
        return result;
    }

    @PostMapping("updateWarehouseOrder")
    @ApiOperation("修改入库单")
    public Result updateWarehouseOrder(@RequestBody CompWarehouseOrderAddDTO compWarehouseOrderAddDTO) {
        Result result = new Result<>();
        CompWarehouseOrderEntity compWarehouseOrderEntity = compWarehouseOrderAddDTO.getCompWarehouseOrderEntity();
        List<CompWarehouseOrderDetailEntity> compWarehouseOrderDetailDTOList =  compWarehouseOrderAddDTO.getCompWarehouseOrderDetailEntityList();
        for (CompWarehouseOrderDetailEntity entity: compWarehouseOrderDetailDTOList){
            CompMacAddDTO compMacAddDTO = compMacService.getMacById(compWarehouseOrderEntity.getCompId(), entity.getMacId());
            CompMacEntity compMacEntity =ConvertUtils
                    .sourceToTarget(compMacAddDTO.getCompMacEntity(), CompMacEntity.class);
            // 出库后,库存数量小于最小库存数量
            if (!StringUtils.isEmpty(compMacEntity)){
                if ((compMacEntity.getMaxInventory() - compMacEntity.getStock()) < entity.getWarehouseNum()){
                    return result.error("库存数量不能小于最小库存数量");
                }else {
                    CompMacEntity compMacEntity1 = new CompMacEntity();
                    compMacEntity1.setId(compMacEntity.getId());
                    compMacEntity1.setCompId(compMacEntity.getCompId());
                    compMacEntity1.setStock(compMacEntity.getStock() + entity.getWarehouseNum());
                    compWarehouseOrderService.updateMacStock(compMacEntity1);
                }
            }else {
                return result.error("设备不存在");
            }


        }
        compWarehouseOrderService.updateWarehouseOrder(compWarehouseOrderAddDTO);
        return result;
    }

    @PostMapping("deleteWarehouseOrder")
    @ApiOperation("删除入库单")
    public Result deleteWarehouseOrder(@RequestBody CompWarehouseOrderDTO compWarehouseOrderDTO) {
        Result result = new Result<>();
        compWarehouseOrderService.deleteWarehouseOrder(compWarehouseOrderDTO);
        return result;
    }

    @PostMapping("getWarehouseOrderDetailPageList")
    @ApiOperation("获取入库单详情分页")
    public Result getWarehouseOrderDetailPageList(@RequestBody CompWarehouseOrderDetailDTO compWarehouseOrderDetailDTO) {
        PageInfo<CompWarehouseOrderDetailDTO> compWarehouseOrderDetailDTOPageInfo = compWarehouseOrderService.getWarehouseOrderDetailPageList(compWarehouseOrderDetailDTO);
        return new Result<PageInfo<CompWarehouseOrderDetailDTO>>().ok(compWarehouseOrderDetailDTOPageInfo);
    }

}
